package com.woniuxy.dudugua.controller;

import com.woniuxy.dudugua.entity.ProductScan;
import com.woniuxy.dudugua.entity.User;
import com.woniuxy.dudugua.service.impl.ProductScanServiceImpl;
import com.woniuxy.dudugua.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("productScan")
public class ProductScanController {
    @Resource
    private ProductScanServiceImpl productScanServiceImpl;
    @RequestMapping("addScan")
    public Result addScan(int id){
        Subject subject = SecurityUtils.getSubject();
        User user = (User)subject.getPrincipal();
        ProductScan productScan = productScanServiceImpl.selectScanById(id, user.getId());
        if(productScan==null){
            ProductScan scan = new ProductScan();
            scan.setProductId(id);
            scan.setUserId(user.getId());
            scan.setTimes(1);
            productScanServiceImpl.insertScan(scan);
        }else{
            productScan.setTimes(productScan.getTimes()+1);
            productScanServiceImpl.updateScan(productScan);
        }
        return new Result("200","success",null,null);
    }
}
